package view;

import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * The main JPanel for the web crawler JFrame.
 * 
 * @author Thomas Nunn
 * @author David Everitt
 * @author Dexter Hu
 * @version TCSS 422 Operating Systems Winter 2014
 */
public class CrawlPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The Dimension of the main panel.
	 */
	private static final Dimension PANEL_SIZE = new Dimension(955, 655);

	/**
	 * Button to start the web crawler.
	 */
	final JButton start = new JButton("Unleash the Crawler");
	
	/**
	 * Button to stop the web crawler.
	 */
	final JButton stop = new JButton("Stop the Crawler");
	
	/**
	 * Button to display the URLs parsed.
	 */
	final JButton urlButton = new JButton("Show URLs");
	
	/**
	 * Button to display the crawler's results.
	 */
	final JButton resultButton = new JButton("Results");
	
	/**
	 * Button to reset the crawler.
	 */
	final JButton resetButton = new JButton("Reset Crawler");
	
	/**
	 * Button to display the web pages retrieved.
	 */
	final JButton pagesRetrievedButton = new JButton("Pages Retrieved");

	/**
	 * Text field for entering a URL.
	 */
	final JTextField urlField = new JTextField();
	
	/**
	 * Text field for entering key words.
	 */
	final JTextField keysField = new JTextField();
	
	/**
	 * Button for selecting single thread option.
	 */
	JRadioButton single = new JRadioButton("Single Thread");
	
	/**
	 * Text field for displaying the crawler's results.
	 */
	//final JTextArea displayField = new JTextArea();
	final JTextArea displayField = new JTextArea();
	
	/**
	 * Constructor
	 */
	public CrawlPanel() {
		setPreferredSize(PANEL_SIZE);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		stop.setEnabled(false);
		urlButton.setEnabled(false);
		resultButton.setEnabled(false);
		resetButton.setEnabled(false);
		pagesRetrievedButton.setEnabled(false);
		buildMainPanel();
		
	}
	
	/**
	 * Builds the CrawlPanel
	 */
	private void buildMainPanel() {
		
		JPanel topPanel = new JPanel();
		JLabel welcomeLabel = new JLabel("<html><font face=\"Arial\", size=\"5\", color=\"#BF00FF\">Welcome," +
				" please enter a valid URL and keywords to search for.</font><html>");
		welcomeLabel.setPreferredSize(new Dimension(580, 60));
		topPanel.add(welcomeLabel);
		
		JPanel inputPanel = buildInputPanel();
		JPanel outputPanel = buildOutputPanel();
		
		add(topPanel);
		add(inputPanel);
		add(outputPanel);
	}
	
	/**
	 * Builds the JPanel which contains the user input elements.
	 * @return The InputPanel
	 */
	private JPanel buildInputPanel() {
		final JPanel result = new JPanel();
		result.setLayout(new BoxLayout(result, BoxLayout.Y_AXIS));

		// URL
		final JPanel urlPanel = new JPanel();
		final JLabel urlLabel = new JLabel("<html><br>URL:</html>");
		urlField.setPreferredSize(new Dimension(530, 30));
		final JLabel instructURLLabel = new JLabel("<html>* Example : <font face=\"Arial\", size=\"4\"," +
				" color=\"#BF00FF\">http://yourpage.com/</font></html>");
		urlPanel.add(urlLabel);
		urlPanel.add(urlField);
		urlPanel.add(instructURLLabel);
		
		// KeyWords
		final JPanel keysPanel = new JPanel();
		final JLabel keysLabel = new JLabel("<html>Keywords:</html>");
		keysField.setPreferredSize(new Dimension(530, 90));
		final JLabel instructKeysLabel = new JLabel("<html>* Separate the keywords with commas." +
				"<br>* Example : <font face=\"Arial\", size=\"4\", color=\"#BF00FF\">cat, dog, mouse," +
				" horse, giraffe</font></html>");
		keysPanel.add(keysLabel);
		keysPanel.add(keysField);
		keysPanel.add(instructKeysLabel);
		
		// radio buttons for single or multithread
		JPanel radioPanel = new JPanel();
		
		// holds various JButtons
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(start);
		buttonPanel.add(stop);
		buttonPanel.add(pagesRetrievedButton);
		buttonPanel.add(urlButton);
		buttonPanel.add(resultButton);
		buttonPanel.add(resetButton);
		
		// add radio buttons to group and panel
		single.setSelected(true);
		JRadioButton multi = new JRadioButton("Multi-Thread");
		final ButtonGroup group = new ButtonGroup();
		group.add(single);
		group.add(multi);
		radioPanel.add(single);
		radioPanel.add(multi);
		
		// add sub-panels to inputPanel
		result.add(urlPanel);
		result.add(keysPanel);
		result.add(radioPanel);
		result.add(buttonPanel);
		
		return result;
	}
	
	/**
	 * Builds the JPanel which contains the WebCrawler's output.
	 * @return The OutputPanel
	 */
	private JPanel buildOutputPanel() {
		JPanel result = new JPanel();
		
		final JLabel displayLabel = new JLabel("Results");
		
		final JScrollPane scroll = new JScrollPane(displayField);
		scroll.setPreferredSize(new Dimension(800, 350));
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.getViewport().add(displayField);
		
		result.add(displayLabel);
		result.add(scroll);
		
		return result;
	}
	
	/**
	 * @return Get the start button.
	 */
	public JButton getStartButton() {
		return start;
	}
	
	/**
	 * @return Get the stop button.
	 */
	public JButton getStopButton() {
		return stop;
	}
	/**
	 * @return Get the URL button.
	 */
	public JButton getURLButton() {
		return urlButton;
	}
	
	/**
	 * @return Get the result button.
	 */
	public JButton getResultButton() {
		return resultButton;
	}

	/**
	 * @return Get the reset button.
	 */
	public JButton getResetButton() {
		return resetButton;
	}
	
	/**
	 * @return Get the pagesRetrievedButton.
	 */
	public JButton getPagesButton() {
		return pagesRetrievedButton;
	}

	/**
	 * @return urlField
	 */
	public JTextField getURLField() {
		return urlField;
	}

	/**
	 * @return keysField
	 */
	public JTextField getKeysField() {
		return keysField;
	}
	
	/**
	 * @return True if the single thread radio button is selected.
	 */
	public boolean isSingleSelected() {
		return single.isSelected();
	}
	
	/**
	 * @param output Sets the displayField text.
	 */
	public void setOutputText(String output) {
		displayField.setText(output);
	}

}
